var DeptInfoDlg = {
    data: {
        pid: "",
        pName: ""
    }
};

layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 档案信息表管理
     */
    var ArchivalInformation = {
        tableId: "archivalInformationTable"
    };

    /**
     * 初始化表格的列
     */
    ArchivalInformation.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: 'center', hide: true, title: ''},
            {field: 'workerNo', align: 'center', sort: true, title: '员工工号'},
            {field: 'name', align: 'center', sort: true, title: '员工姓名'},
            {field: 'phone', align: 'center', sort: true, title: '手机号'},
            {field: 'deptName', align: 'center', sort: true, title: '所属部门'},
            {field: 'education', align: 'center', sort: true, title: '学历'},
            {field: 'age', align: 'center', sort: true, title: '年龄'},
            {field: 'entryDate', align: 'center', sort: true, title: '入职日期'},
            {field: 'incumbency', align: 'center', sort: true, title: '员工类型'},
            {field: 'createTime', align: 'center', sort: true, title: '创建时间'},
            // {field: 'createUser', align: 'center', sort: true, title: '创建人'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    // 点击选择所属单位时
    $('#deptName').click(function () {
        var formName = encodeURIComponent("parent.DeptInfoDlg.data.pName");
        var formId = encodeURIComponent("parent.DeptInfoDlg.data.pid");
        var treeUrl = encodeURIComponent("/dept/tree");

        layer.open({
            type: 2,
            title: '所属部门',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#deptId").val(DeptInfoDlg.data.pid);
                $("#deptName").val(DeptInfoDlg.data.pName);
            }
        });
    });

    /**
     * 点击查询按钮
     */
    ArchivalInformation.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        queryData['incumbency'] = $("#incumbency").val();
        queryData['deptId'] = $("#deptId").val();
        table.reload(ArchivalInformation.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    ArchivalInformation.openAddDlg = function () {
        func.open({
            title: '添加档案信息',
            content: Feng.ctxPath + '/archivalInformation/add',
            tableId: ArchivalInformation.tableId,
            maxmin:true
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    ArchivalInformation.openEditDlg = function (data) {
        func.open({
            title: '修改档案信息',
            content: Feng.ctxPath + '/archivalInformation/edit?id=' + data.id,
            tableId: ArchivalInformation.tableId,
            maxmin:true
        });
    };

    /**
     * 导出excel按钮
     */
    ArchivalInformation.exportExcel = function () {
        var checkRows = table.checkStatus(ArchivalInformation.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ArchivalInformation.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archivalInformation/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ArchivalInformation.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ArchivalInformation.tableId,
        url: Feng.ctxPath + '/archivalInformation/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: ArchivalInformation.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ArchivalInformation.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ArchivalInformation.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ArchivalInformation.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ArchivalInformation.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ArchivalInformation.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ArchivalInformation.onDeleteItem(data);
        }
    });
});
